﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Input;
using DC2010;
using DC2010.Objects;
using DC2010.Effects;
using DC2010.Objects.Data;

namespace RolePlaying.Events
{
    public class EventsDungeon
    {
        public EventsDungeon()
        {
            RM.Get("Dungeon").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(RM_OnDungeonLeft);
            RM.Get("Dungeon").OnMouseRight += new ActiveRectangle.mouseStateDelegate(RM_OnDungeonRight);

            RM.Get("Dungeon_Floor_02_Left").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(EventsDungeon_OnMouseLeft02L);
            RM.Get("Dungeon_Floor_02_Right").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(EventsDungeon_OnMouseLeft02R);

            RM.Get("Dungeon_Floor_12_Left_Near").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(EventsDungeon_OnMouseLeft12L);
            RM.Get("Dungeon_Floor_12_Right_Near").OnMouseLeft += new ActiveRectangle.mouseStateDelegate(EventsDungeon_OnMouseLeft12R);

        }

        void EventsDungeon_OnMouseLeft02L(MouseState state, ActiveRectangle ar)
        {
            //get tile we are standing on
            DungeonLoc l = Root.I.GameSystem.Party.Location;
            //Tile t = Root.I.IS.GetMap(l.Floor).GetTile(l.X, l.Y);

            //HandleDungeonItem(ObjectPosition.NorthWest,t);
        }

        void EventsDungeon_OnMouseLeft02R(MouseState state, ActiveRectangle ar)
        {
            //get tile we are standing on
            DungeonLoc l = Root.I.GameSystem.Party.Location;
            //Tile t = Root.I.IS.GetMap(l.Floor).GetTile(l.X, l.Y);

            //HandleDungeonItem(ObjectPosition.NorthEast,t);
        }

        void EventsDungeon_OnMouseLeft12L(MouseState state, ActiveRectangle ar)
        {
            //get tile we are standing on
            DungeonLoc l = Root.I.GameSystem.Party.Location;
            //Tile t = Root.I.MovementSystem.Get12();

            //HandleDungeonItem(ObjectPosition.SouthWest, t);
        }

        void EventsDungeon_OnMouseLeft12R(MouseState state, ActiveRectangle ar)
        {
            //get tile we are standing on
            DungeonLoc l = Root.I.GameSystem.Party.Location;
            //Tile t = Root.I.MovementSystem.Get12();

            //HandleDungeonItem(ObjectPosition.SouthEast, t);
        }

        public void HandleDungeonItem(ObjectPosition positionWeTouch, TileData t)
        {
            //if ((Root.I.GameSystem.CurrentScreen == "Dungeon") || (Root.I.GameSystem.CurrentScreen == null))
            //{
            //    //recalculate position
            //    ObjectPosition op = Root.I.MovementSystem.GetCornerFrontInverse(positionWeTouch);


            //    DungeonPlacement dpTemp = null;
            //    foreach (DungeonPlacement dp in t.Info.Items)
            //    {
            //        if (dp.ObjectPosition == op)
            //        {
            //            dpTemp = dp;
            //        }
            //    }

            //    if (dpTemp != null)
            //    {
            //        //do we have free hand?
            //        if (Root.I.GameSystem.ItemInHand == null)
            //        {
            //            //take from ground
            //            t.Info.Items.Remove(dpTemp);

            //            //put it in your hand
            //            Root.I.GameSystem.ItemInHand = Root.I.IS.GetItem(dpTemp.ObjectId);
            //        }
            //        else
            //        {
            //            //put item on ground
            //            DungeonPlacement dp = new DungeonPlacement();
            //            dp.ObjectPosition = op;
            //            dp.ObjectId = Root.I.GameSystem.ItemInHand.Info.Id;
            //            t.Info.Items.Add(dp);
            //            Root.I.GameSystem.ItemInHand = null;

            //        }


            //    }
            //    else
            //    {
            //        //nothing on that spot. do we have anything in hand?
            //        if (Root.I.GameSystem.ItemInHand != null)
            //        {
            //            DungeonPlacement dp = new DungeonPlacement();
            //            dp.ObjectPosition = op;
            //            dp.ObjectId = Root.I.GameSystem.ItemInHand.Info.Id;
            //            t.Info.Items.Add(dp);
            //            Root.I.GameSystem.ItemInHand = null;
            //        }
            //    }
            //}
        }

        void RM_OnDungeonLeft(MouseState state, ActiveRectangle ar)
        {
            //if ((Root.I.GameSystem.CurrentScreen == "Dungeon") || (Root.I.GameSystem.CurrentScreen == null))
            //{
            //    //get tile in fron of me.
            //    TileData t = Root.I.MovementSystem.Get12();

            //    //are there any items to handle?
            //    if (t.Info.Items.Count > 0)
            //    {
            //        ItemData iInHand = Root.I.GameSystem.ItemInHand;

            //        foreach (DungeonPlacement dp in t.Info.Items)
            //        {
            //            //are we looking at this object?
            //            if (dp.ObjectPosition == (ObjectPosition)(int)Root.I.GameSystem.Party.Info.Loc.FacingOposite)
            //            {
            //                //get this object
            //                Item i = Root.I.IS.GetItem(dp.ObjectId);

            //                if (i != null)
            //                {
            //                    //execute effects on that
            //                    foreach (SimpleEffect key in i.Info.OnUseEffects)
            //                    {
            //                        Effect ef = Root.I.IS.InstantiatedEffect(key);
            //                        ef.Instant(iInHand, i);
            //                    }
            //                }

            //                //handle item picking.
            //                //do we have free hand?
            //                if (Root.I.GameSystem.ItemInHand == null)
            //                {
            //                    //nothing in hand...put item into hand
            //                    if (i.Info.IsContainerItem)
            //                    {
            //                        //get first available item
            //                        string sRemovedItemId = i.Info.ContainerItems.RemoveItemFromLastPosition();

            //                        //put it in your hand
            //                        Root.I.GameSystem.ItemInHand = Root.I.IS.GetItem(sRemovedItemId);
            //                    }
            //                }
            //                else
            //                {
            //                    //we have something in hand, put it in collection
            //                    i.Info.ContainerItems.PutItemToFirstFreePosition(iInHand.Info.Id);
                                
            //                    // clear hand
            //                    Root.I.GameSystem.ItemInHand = null;

            //                }


            //            }
            //        }
            //    }
            //}
        }

        void RM_OnDungeonRight(MouseState state, ActiveRectangle ar)
        {
            if (Root.I.GameSystem.CurrentScreen == "Dungeon")
            {

            }
        }

    }
}
